<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>

    <style>

        .viewer-ruler-wire-highlighted {
            border: 2px solid white !important;
        }

        .viewer-ruler-label-highlighted {
            border: 2px solid white !important;
        }

        .viewer-ruler-dot-highlighted {
            border: 2px solid white !important;
        }

        .xeokit-context-menu {
            font-family: 'Roboto', sans-serif;
            font-size: 15px;
            display: none;
            z-index: 300000;
            background: rgba(255, 255, 255, 0.46);
            border: 1px solid black;
            border-radius: 6px;
            padding: 0;
            width: 200px;
        }

        .xeokit-context-menu ul {
            list-style: none;
            margin-left: 0;
            padding: 0;
        }

        .xeokit-context-menu-item {
            list-style-type: none;
            padding-left: 10px;
            padding-right: 20px;
            padding-top: 4px;
            padding-bottom: 4px;
            color: black;
            background: rgba(255, 255, 255, 0.46);
            cursor: pointer;
            width: calc(100% - 30px);
        }

        .xeokit-context-menu-item:hover {
            background: black;
            color: white;
            font-weight: normal;
        }

        .xeokit-context-menu-item span {
            display: inline-block;
        }

        .xeokit-context-menu .disabled {
            display: inline-block;
            color: gray;
            cursor: default;
            font-weight: normal;
        }

        .xeokit-context-menu .disabled:hover {
            color: gray;
            cursor: default;
            background: #eeeeee;
            font-weight: normal;
        }

        .xeokit-context-menu-item-separator {
            background: rgba(0, 0, 0, 1);
            height: 1px;
            width: 100%;
        }

        

    </style>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div class="slideout-sidebar">
    <img class="info-icon" src="../../assets/images/measure_distance_icon.png"/>
    <h1>DistanceMeasurementPlugin with DistanceMeasurementsMouseControl and PointerLens</h1>
    <h2>Click on the model to create distance measurements, with no vertex and edge snapping</h2>
    <p>In this example, we're using a programmatically-created SceneModel with objects made of points, lines and
        triangles, using data texture
        scene representation and batched geometries.</p>
    <h3>Components Used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/DistanceMeasurementsPlugin/DistanceMeasurementsPlugin.js~DistanceMeasurementsPlugin.html"
               target="_other">DistanceMeasurementsPlugin</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/DistanceMeasurementsPlugin/DistanceMeasurementsMouseControl.js~DistanceMeasurementsMouseControl.html"
               target="_other">DistanceMeasurementsMouseControl</a>
        </li>
        <li>
            <a href="../../docs/class/src/extras/PointerLens/PointerLens.js~PointerLens.html"
               target="_other">PointerLens</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
               target="_other">XKTLoaderPlugin</a>
        </li>
    </ul>
    <h3>Tutorials</h3>
    <ul>
        <li>
            <a href="https://www.notion.so/xeokit/Accurate-Measurements-with-Snapping-5e6606afef20428f9b319ea0e82270f9"
               target="_other">Accurate Measurements with Snapping</a>
        </li>
    </ul>
</div>
</body>
<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {
        Viewer, buildSphereGeometry,
        buildBoxGeometry,
        buildCylinderGeometry,
        buildTorusGeometry,
        buildBoxLinesGeometry,
        buildGridGeometry,
        buildVectorTextGeometry,
        ContextMenu,
        DistanceMeasurementsPlugin,
        DistanceMeasurementsMouseControl,
        SceneModel,
        PointerLens
    } from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer and arrange the camera
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        dtxEnabled: true
    });

    viewer.scene.camera.eye = [-10.970825516832445, 12.854221661021146, 24.950601235099196];
    viewer.scene.camera.look = [8.500572204589844, 2.190000057220459, 0];
    viewer.scene.camera.up = [0.18, 0.93, -0.25];

    viewer.cameraControl.navMode = "orbit";
    viewer.cameraControl.followPointer = true;

    viewer.cameraControl.panRightClick = false; // Prevents right-click-drag panning interfering with ContextMenus

    viewer.scene.pointsMaterial.roundPoints = true;
    viewer.scene.pointsMaterial.perspectivePoints = true;
    viewer.scene.pointsMaterial.pointSize = 2;
    viewer.scene.pointsMaterial.minPerspectivePointSize = 2;
    viewer.scene.pointsMaterial.maxPerspectivePointSize = 6;

    viewer.scene.linesMaterial.lineWidth = 1;

    //------------------------------------------------------------------------------------------------------------------
    // Create a SceneModel containing an assortment of the available geometry primitive types
    //------------------------------------------------------------------------------------------------------------------

    const sceneModel = new SceneModel(viewer.scene, {
        id: "myGeometries"
    });

    // Box triangles

    const box = buildBoxGeometry({
        xSize: 1,
        ySize: 1,
        zSize: 1
    });


    sceneModel.createMesh({
        id: "boxMesh",
        primitive: "triangles",
        positions: box.positions,
        normals: box.normals,
        indices: box.indices,
        position: [0, 0, 0],
        color: [1, 0, 0]
    });

    // Box lines

    const boxLines = buildBoxLinesGeometry({
        xSize: 1,
        ySize: 1,
        zSize: 1
    });

    sceneModel.createMesh({
        id: "boxLinesMesh",
        primitive: "lines",
        positions: boxLines.positions,
        indices: boxLines.indices,
        position: [3, 0, 0],
        color: [0, 0, 1]
    });

    // Sphere triangles

    const sphere = buildSphereGeometry({
        center: [0, 0, 0],
        radius: 1.5,
        heightSegments: 60,
        widthSegments: 60
    });

    sceneModel.createMesh({
        id: "sphereMesh",
        primitive: "triangles",
        positions: sphere.positions,
        normals: sphere.normals,
        indices: sphere.indices,
        position: [7, 0, 0],
        color: [0, 0.5, 1]
    });

    // Torus triangles

    const torus = buildTorusGeometry({
        center: [0, 0, 0],
        radius: 1.5,
        tube: 0.5,
        radialSegments: 32,
        tubeSegments: 24,
        arc: Math.PI * 2.0
    });

    sceneModel.createMesh({
        id: "torusMesh",
        primitive: "triangles",
        positions: torus.positions,
        normals: torus.normals,
        indices: torus.indices,
        position: [11, 0, 0],
        color: [0.7, 0, 1]
    });

    // Cylinder triangles

    const cylinder = buildCylinderGeometry({
        center: [0, 0, 0],
        radiusTop: 1.0,
        radiusBottom: 2.0,
        height: 2.5,
        radialSegments: 20,
        heightSegments: 1,
        openEnded: false
    });

    sceneModel.createMesh({
        id: "cylinderMesh",
        primitive: "triangles",
        positions: cylinder.positions,
        normals: cylinder.normals,
        indices: cylinder.indices,
        position: [16, 0, 0],
        color: [1, .6, 0]
    });

    // Grid lines

    const grid = buildGridGeometry({
        size: 10,
        divisions: 10
    });

    sceneModel.createMesh({
        id: "gridMesh",
        primitive: "lines",
        positions: grid.positions,
        indices: grid.indices,
        position: [25, 0, 0],
        color: [0, 1, 0]
    });

    // Text

    const text = buildVectorTextGeometry({
        origin: [0, 0, 0],
        text: "Mouse over this text and these objects\nto create distance measurements\nwith cursor snapping and data textures",
    });

    sceneModel.createMesh({
        id: "textMesh",
        primitive: "lines",
        positions: text.positions,
        indices: text.indices,
        position: [0, 7.5, 0],
        color: [0, 0, 0]
    });

    // Points

    const positions = [];
    const colors = [];

    const map = {};

    for (let i = 0; i < 2000;) {

        const x = Math.random();
        const y = Math.random();
        const z = Math.random();

        const hash = "" + x + "." + y + "." + z;
        if (map[hash]) {
            continue;
        }

        map[hash] = true;

        positions.push(x);
        positions.push(y);
        positions.push(z);

        colors.push(Math.random());
        colors.push(Math.random());
        colors.push(Math.random());
        colors.push(Math.random());

        i++;
    }

    sceneModel.createMesh({
        id: "pointsMesh",
        primitive: "points",
        positions,
        colors,
        position: [-7, 0, 0],
        scale: [4, 4, 4],
        rotation: [0, 0, 0]
    });

    // One entity contains all our meshes

    sceneModel.createEntity({
        id: "geometries",
        meshIds: ["boxMesh", "boxLinesMesh", "sphereMesh", "torusMesh", "cylinderMesh", "gridMesh", "textMesh", "pointsMesh"]
    });

    sceneModel.finalize();

    //------------------------------------------------------------------------------------------------------------------
    // DistanceMeasurementsPlugin, DistanceMeasurementsMouseControl and PointerLens
    //------------------------------------------------------------------------------------------------------------------

    const distanceMeasurementsPlugin = new DistanceMeasurementsPlugin(viewer);

    distanceMeasurementsPlugin.on("mouseOver", (e) => {
        e.distanceMeasurement.setHighlighted(true);
    });

    distanceMeasurementsPlugin.on("mouseLeave", (e) => {
        if (distanceMeasurementsContextMenu.shown && distanceMeasurementsContextMenu.context.distanceMeasurement.id === e.distanceMeasurement.id) {
            return;
        }
        e.distanceMeasurement.setHighlighted(false);
    });

    const distanceMeasurementsMouseControl = new DistanceMeasurementsMouseControl(distanceMeasurementsPlugin, {
        pointerLens: new PointerLens(viewer)
    })

    distanceMeasurementsMouseControl.snapping = true;

    distanceMeasurementsMouseControl.activate();

    //------------------------------------------------------------------------------------------------------------------
    // Create a context menu to delete and configure measurements
    //------------------------------------------------------------------------------------------------------------------

    const distanceMeasurementsContextMenu = new ContextMenu({
        items: [
            [
                {
                    title: "Clear",
                    doAction: function (context) {
                        context.distanceMeasurement.destroy();
                    }
                },
                {
                    getTitle: (context) => {
                        return context.distanceMeasurement.axisVisible ? "Hide Axis" : "Show Axis";
                    },
                    doAction: function (context) {
                        context.distanceMeasurement.axisVisible = !context.distanceMeasurement.axisVisible;
                    }
                },
                {
                    getTitle: (context) => {
                        return context.distanceMeasurement.xLabelEnabled && context.distanceMeasurement.labelsVisible ? "Disable X Label" : "Enable X Label";
                    },
                    doAction: function (context) {
                        context.distanceMeasurement.xLabelEnabled = !context.distanceMeasurement.xLabelEnabled;
                    }
                },
                {
                    getTitle: (context) => {
                        return context.distanceMeasurement.yLabelEnabled && context.distanceMeasurement.labelsVisible ? "Disable Y Label" : "Enable Y Label";
                    },
                    doAction: function (context) {
                        context.distanceMeasurement.yLabelEnabled = !context.distanceMeasurement.yLabelEnabled;
                    }
                },
                {
                    getTitle: (context) => {
                        return context.distanceMeasurement.zLabelEnabled && context.distanceMeasurement.labelsVisible ? "Disable Z Label" : "Enable Z Label";
                    },
                    doAction: function (context) {
                        context.distanceMeasurement.zLabelEnabled = !context.distanceMeasurement.zLabelEnabled;
                    }
                },
                {
                    getTitle: (context) => {
                        return context.distanceMeasurement.lengthLabelEnabled && context.distanceMeasurement.labelsVisible ? "Disable Length Label" : "Enable Length Label";
                    },
                    doAction: function (context) {
                        context.distanceMeasurement.lengthLabelEnabled = !context.distanceMeasurement.lengthLabelEnabled;
                    }
                },
                {
                    getTitle: (context) => {
                        return context.distanceMeasurement.labelsVisible ? "Hide All Labels" : "Show All Labels";
                    },
                    doAction: function (context) {
                        context.distanceMeasurement.labelsVisible = !context.distanceMeasurement.labelsVisible;
                    }
                }
            ], [
                {
                    title: "Clear All",
                    getEnabled: function (context) {
                        return (Object.keys(context.distanceMeasurementsPlugin.measurements).length > 0);
                    },
                    doAction: function (context) {
                        context.distanceMeasurementsPlugin.clear();
                    }
                },
                {
                    getTitle: () => {
                        return "Cancel Measurement";
                    },
                    getEnabled: function () {
                        return (!!distanceMeasurementsMouseControl.currentMeasurement);
                    },
                    doAction: function () {
                        distanceMeasurementsMouseControl.reset();
                    }
                }
            ]
        ]
    });

    distanceMeasurementsContextMenu.on("hidden", () => {
        if (distanceMeasurementsContextMenu.context.distanceMeasurement) {
            distanceMeasurementsContextMenu.context.distanceMeasurement.setHighlighted(false);
        }
    });

    //------------------------------------------------------------------------------------------------------------------
    // Create a context menu to activate and deactivate the control
    //------------------------------------------------------------------------------------------------------------------

    const canvasContextMenu = new ContextMenu({
        enabled: true,
        context: {
            viewer: viewer
        },
        items: [
            [
                {
                    getTitle: (context) => {
                        return distanceMeasurementsMouseControl.active ? "Deactivate Control" : "Activate Control";
                    },
                    doAction: function (context) {
                        distanceMeasurementsMouseControl.active
                            ? distanceMeasurementsMouseControl.deactivate()
                            : distanceMeasurementsMouseControl.activate();
                    }
                },
                {
                    getTitle: () => {
                        return "Cancel Measurement";
                    },
                    getEnabled: function () {
                        return (!!distanceMeasurementsMouseControl.currentMeasurement);
                    },
                    doAction: function () {
                        distanceMeasurementsMouseControl.reset();
                    }
                }
            ]
        ]
    });

    // viewer.cameraControl.on("rightClick", function (e) {
    //     canvasContextMenu.show(e.pagePos[0], e.pagePos[1]);
    //     e.event.preventDefault();
    // });

    //------------------------------------------------------------------------------------------------------------------
    // Create an DistanceMeasurementsPlugin, activate its DistanceMeasuremntsControl
    //------------------------------------------------------------------------------------------------------------------

    distanceMeasurementsPlugin.on("mouseOver", (e) => {
        e.distanceMeasurement.setHighlighted(true);
    });

    distanceMeasurementsPlugin.on("mouseLeave", (e) => {
        if (distanceMeasurementsContextMenu.shown && distanceMeasurementsContextMenu.context.distanceMeasurement.id === e.distanceMeasurement.id) {
            return;
        }
        e.distanceMeasurement.setHighlighted(false);
    });

    distanceMeasurementsPlugin.on("contextMenu", (e) => {
        distanceMeasurementsContextMenu.context = { // Must set context before showing menu
            viewer: viewer,
            distanceMeasurementsPlugin: distanceMeasurementsPlugin,
            distanceMeasurement: e.distanceMeasurement
        };
        distanceMeasurementsContextMenu.show(e.event.clientX, e.event.clientY);
        e.event.preventDefault();
    });

    window.viewer = viewer;

</script>
</html>